﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace KLTN
{
   public class Pendulum
    {
       public double AngleBegin { get; set; }
       public double Angle0 { get; set; }
       public double AngleAfter { get; set; }
       public float Lenght { get; set; }
       public Vector3 Position { get; set; }
       public CModel Model;
       
       private float _time;
       CCamera camera;
       public Pendulum(CModel Model,double Angle0 ,double AngleBegine,float Lenght, Vector3 Position)
       { 
           this.Angle0= Angle0;
           this.Lenght= Lenght;
           this.Model= Model;
           this.AngleBegin = AngleBegin;
           this.Position = Position;
       }
       public void Move(GameTime gameTime)
       {
           _time += (float)gameTime.ElapsedGameTime.Milliseconds / 1000;
           AngleAfter = Angle0 * Math.Cos(Math.Sqrt(Const.GRAVITY / Lenght) * _time+AngleBegin);
           Model.Rotation = new Vector3((float)AngleAfter,0,0);//cho xoay 
           Model.Positon = Position;

       }
       public void LoadContent(ContentManager Content, GraphicsDevice graphicDevice)
       {
           camera = new FreeCamera(new Vector3(2000, 0, 0), MathHelper.ToRadians(153), MathHelper.ToRadians(5), graphicDevice);
       }
       public void Update(GameTime gameTime)
       {
           UpdateCamera();
           Move(gameTime);
       }
       public void Draw()
       {
           if (camera.BoudingVolumeslnView(Model.BoudingSphere))
           {
               Model.Draw(camera.View,camera.Projection);
           }
       }

       public void UpdateCamera()
       {
           KeyboardState keystate = Keyboard.GetState();
           if (keystate.IsKeyDown(Keys.Left))
               ((FreeCamera)camera).Move(Vector3.Left * 10);
           if (keystate.IsKeyDown(Keys.Right))
               ((FreeCamera)camera).Move(Vector3.Right * 10);
           if (keystate.IsKeyDown(Keys.Up))
               ((FreeCamera)camera).Move(Vector3.Up * 10);
           if (keystate.IsKeyDown(Keys.Down))
               ((FreeCamera)camera).Move(Vector3.Down * 10);
           camera.Update();
       }
    }
}
